<?php

namespace app\common\utils;

use app\common\constant\CommonConstants;

/**
 * HTTP请求类
 */
class HttpUtils
{
    public static function getRequestData($url, $method = 'get', $data = null, $headers = [], $extra = [])
    {
        $curl = self::sendRequest($url, $method, $data, $headers, $extra);
        //返回返回状态码，并且校验是否是json
        if ($curl['http_code'] != 200) {
            return [];
        }

        $ret_arr = @json_decode($curl['ret'], true);
        if (!$ret_arr) {
            return [];
        }

        return $ret_arr;
    }

    /**
     * 发送HTTP请求
     * @param $url
     * @param $method
     * @param $data
     * @param $headers
     * @param $extra
     * @return array
     */
    public static function sendRequest($url, $method = 'get', $data = null, $headers = [], $extra = [])
    {
        $curl_start_time = microtime(true);
        $curl_timeout = CommonConstants::CURL_API_TIMEOUT;
        if (!empty($extra['timeout'])) {
            $curl_timeout = $extra['timeout'];
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //获取的信息以文件流的形式返回
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $curl_timeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $curl_timeout); //设置超时限制防止死循环
        if (!empty($headers)) {
            array_push($headers, 'Expect:');
        } else {
            $headers = array('Expect:');
        }
        if (!empty($headers)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        if ($method == 'post') {
            if (!$data) {
                $data = [];
            }
            curl_setopt($ch, CURLOPT_POST, 1); //设置为POST
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }

        $ret = curl_exec($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if (curl_errno($ch)) {
            $error_msg = curl_error($ch);
            $error_no = curl_errno($ch);
        }
        curl_close($ch);

        $curl_end_time = microtime(true);
        $curl_time_cost = intval(($curl_end_time - $curl_start_time) * 1000);

        if ($http_code == 0) {
            $notice_error_msg = "CURL http code返回0; URL:{$url}; Method:{$method}; 请求开始时间:{$curl_start_time}, 请求结束时间:{$curl_end_time}; curl_errno:" . @$error_no . ", 错误详情:" . @$error_msg;
            //todo 将上面的报错信息发送到研发群

            MyLog::record()->error($notice_error_msg, 'curl_http_code_is_0');
        }

        //写入Log
        $url_explode = explode('/', $url);
        $req_host = @$url_explode[2] ?: '';
        $log_data = [
            'req_host' => 'host-' . $req_host,
            'req_url' => $url,
            'req_data' => $data,
            'req_headers' => $headers,
            'req_method' => $method,
            'curl_return' => @json_decode($ret, true),
            'http_code' => $http_code,
            'curl_timeout' => $curl_timeout,
            'time_cost' => $curl_time_cost,
            'error' => @$error_msg,
            'error_no' => @$error_no,
        ];
        if ($extra) {
            $log_data['extra'] = $extra;
        }
        MyLog::record()->info($log_data, 'curl_request');

        return [
            'ret' => $ret,
            'http_code' => $http_code,
            'error' => @$error_msg,
            'error_no' => @$error_no,
        ];
    }
}